package com.waiterxiaoyy.controller;


import com.waiterxiaoyy.entity.User;
import com.waiterxiaoyy.service.CreateUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class SendMailController {

    @Autowired
    private CreateUserService createUserService;


    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("user", new User());
        return "form";
    }

    @GetMapping("form")
    public String uform(Model model) {
        model.addAttribute("user", new User());
        return "form";
    }


    @PostMapping("/create")
    public String createUser(@ModelAttribute User user, Model model) {
        model.addAttribute("url", "/");
        if(createUserService.createUser(user)) {
            model.addAttribute("msg", "添加成功，坐等好消息吧！");
            return "success";
        }
        model.addAttribute("msg", "数据库已存在此邮箱，请重新填写");
        return "error";
    }

    @GetMapping("success")
    public String success() {
        return "success";
    }

    @GetMapping("error")
    public String error() {
        return "error";
    }

    @GetMapping("/admin/cang/de/hen/sen")
    public String admin(Model model) {
        model.addAttribute("users", createUserService.select(1));
        model.addAttribute("notices", createUserService.select(2));
        model.addAttribute("renews", createUserService.select(3));
        return "admin";
    }
}
